package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/4/12 16:27
 */
@Api(tags = "SPU列表查询接口")
@RestController // @ResponseBody + @Controller
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    //  http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61 {"category3Id":61}
    //  @GetMapping("{page}/{limit}?category3Id=61")    //  不用！ HttpServletRequest request String category3Id SpuInfo spuInfo
    //  springmvc 有个知识点，对象方式传值！
    //  获取三级分类Id 有n种方式！
    // 根据查询条件封装控制器
    // springMVC 的时候，有个叫对象属性传值 如果页面提交过来的参数与实体类的参数一致，
    // 则可以使用实体类来接收数据
    // http://api.gmall.com/admin/product/1/10?category3Id=61
    // @RequestBody 作用 将前台传递过来的json{"category3Id":"61"}  字符串变为java 对象。
    @ApiOperation(value = "获取spu分页列表")
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo){
        //分页查询
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);
        //调用服务层方法
        IPage<SpuInfo> iPage = manageService.getSpuInfoPage(spuInfoPage,spuInfo);
        return Result.ok(iPage);
    }


    //http://api.gmall.com/admin/product/baseSaleAttrList
    @ApiOperation(value = "获取销售属性")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        // 查询所有的销售属性集合
       List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
       return Result.ok(baseSaleAttrList);
    }

    //http://api.gmall.com/admin/product/saveSpuInfo
    @ApiOperation(value = "保存spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //调用服务层的保存方法
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/spuImageList/{spuId}
    @ApiOperation(value = "根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //调用服务层方法
        List<SpuImage> spuImageList =  manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
    @ApiOperation(value = "根据spuId获取销售属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //调用服务层方法
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
